package com.rong.creatmodel.singlemodel.example;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Client {
    public static void main(String[] args) throws Exception {

//        SingleTonEnum e = SingleTonEnum.INSTANCE;
//        SingleTonEnum e1 = SingleTonEnum.INSTANCE;
//        System.out.println(e == e1);

//        Single single = Single.getInstance();
//        writeObjectToFile(single);


        // 测试序列化破坏单例模式
        // 两次获取的对象地址不同从而破坏了单例模式
        readObjectToFile();
        readObjectToFile();




    }

    // 从文件中获取对象（不同的对象）
    public static void readObjectToFile() throws Exception{

        // 创建对象输入流
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("D:\\下载文件\\a.txt"));
        // 读取对象
        Single single = (Single) inputStream.readObject();
        System.out.println(single);
        inputStream.close();

    }

    // 向文件中写入数据（对象）
        public static void writeObjectToFile(Single single) throws Exception{
        // 创建对象输出流
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\下载文件\\a.txt"));
            // 写入对象
            objectOutputStream.writeObject(single);
            // 关闭流
            objectOutputStream.close();

    }


}
